package com.example.thegreatwall.controller;

import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 管理员端建立长连接到后端
 */
@ServerEndpoint(value = "/websocket/{userId}")
@Component
public class MyWebSocket {
    public static Map<String, Session> webSocketMap = new ConcurrentHashMap<>();

    public MyWebSocket() {
        System.out.println("实例化web是多例模式的");
    }
    @OnOpen
    public void onOpen(Session session, @PathParam("userId") String userId)throws IOException {
        System.out.println("userId:就是我我就是:"+userId);
        webSocketMap.put(userId, session);
    }
    @OnError
    public void onError(Session session, Throwable error) {
        error.printStackTrace();
    }
}
